<template>
  <div>
    <el-breadcrumb separator="/" class="rumb">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-table :data="roleList" border stripe>
        <el-table-column type="expand"></el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeRoles(scope.row.id)">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRolesDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>   
    </el-card>
    <el-dialog
      title="分配权限"
      :visible.sync="setDialogVisible"
      width="50%" @close="setRightClose" :modal="false">
      <el-tree
        :props="props"
        :data="rightslist" show-checkbox node-key="id" :default-checked-keys="defKeys">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        roleList: [],
        setDialogVisible: false,
        rightslist: [],
        props: {
          label: 'authName',
          children: 'children'
        },
        defKeys: []
      }
    },
    created() {
      this.getRolesList()
    },
    methods: {
      async getRolesList() {
        const { data:res } = await this.$http.get('roles')

        if(res.meta.status !== 200) {
          return this.$message.error('获取角色列表失败')
        }
        this.roleList = res.data
      },
      async showSetRolesDialog(role) {
        const {data:res} = await this.$http.get('rights/tree')
        if(res.meta.status !== 200) {
          return this.$message.error('获取权限列表失败')
        }
        this.rightslist = res.data
        this.getLeafKeys(role, this.defKeys)
        this.setDialogVisible = true
      },
      //递归函数拿到第三级的id
      getLeafKeys(node, arr) {
        if (!node.children) {
          return arr.push(node.id)
        }
        node.children.forEach(item => this.getLeafKeys(item, arr))
      },
      setRightClose() {
        this.defKeys = []
      },
      async removeRoles(roleId) {
        const {data:res} = await this.$http.delete(`roles/${roleId}`)
        if(res.meta.status !== 200) {
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
        this.getRolesList()
      }
    }
  }
</script>

<style scoped lang="less">

</style>